<?php
/*分页
/*pagination
	num			最大分页数
	perpage		每页多少信息数
	curr_page	当前页数
	pages		分页数量
	mpurl		翻页地址 如果要在翻页ID后加地址比如a.php?action&5&type代码为:a.php?action!!&type
	style		显示风格
example:
<code>
		$sql = "SELECT * FROM videos_list WHERE v_user='$us[username]' ORDER BY ".$orders;
		$info = $db->limit($sql,$page,10);
		$data_list = $info['query'];
		$pages = paging($info['info'],3);
</code>
*/
function paging($info, $style='',$mpurl='') {
  global $_SERVER,$target,$keys;
  //信息接收处理
  $num		= $info['nums'];
  $perpage	= $info['limit'];
  $curr_page= $info['nowpage'];
  $pages 	= $info['pages'];
  
  if($mpurl==''){
  	 $mpurl = preg_replace("/&page=[0-9]+/is","",geturl());
  }

  $mpurl  = explode("!!",$mpurl);
  $mpurl1 = $mpurl[0];
  $mpurl2 = $mpurl[1];
  $mpurl3 = ($mpurl2=='')?'':"+'$mpurl2'";
  
  //根据搜索
  $mpurl2		= $mpurl2.((trim($keys))?'&keys='.$keys:'');
  

  $multipage = '';
	if($num > $perpage) {
		$page = 5;
		$offset = 2;

		$from = $curr_page - $offset;
		$to = $curr_page + $page - $offset - 1;
		if($page > $pages) {
			$from = 1;
			$to = $pages;
		} else {
			if($from < 1) {
				$to = $curr_page + 1 - $from;
				$from = 1;
				if(($to - $from) < $page && ($to - $from) < $pages) {
					$to = $page;
				}
			} else if($to > $pages) {
				$from = $curr_page - $pages + $to;
				$to = $pages;
				if(($to - $from) < $page && ($to - $from) < $pages) {
					$from = $pages - $page + 1;
				}
			}
		}

		switch($style){

		//样式1
		//style
		//共有 192 篇文章 | 10 条/页 首 页 上一页 下一页 末 页
		//totally   passages/10 perpage homepage previous page next page/last page
	 	  case'1':
		    if(($curr_page-1)<=1){$pt= 1;}else{$pt=$curr_page-1;}
		    if(($curr_page+1)>=$pages){$pd=$pages;}else{$pd=$curr_page+1;}
		  	$multipage .= "<b>$curr_page/$pages pages | totally $num | $perpage perpage</b> <a href=\"$mpurl1&page=1$mpurl2\" style=\"line-height: normal\">Main page</a>&nbsp;&nbsp;&nbsp;
		<a href=\"$mpurl1&page=$pt$mpurl2\" style=\"line-height: normal\">Previous</a>&nbsp;&nbsp;&nbsp;
		<a href=\"$mpurl1&page=$pd$mpurl2\" style=\"line-height: normal\">Next</a>&nbsp;&nbsp;&nbsp;
		<a href=\"$mpurl1&page=$pages$mpurl2\" style=\"line-height: normal\">Last</a>";
		  break;

		//样式2
		//style 2
		//[ 页次: 6/20 页, 共有 192 条信息, 每页 10 条信息 ] 9 7 4 5 6 7 8 8 : 转到:{}
		//[ page: 6/20 , totally 192 messages,  10 messages per page ] 9 7 4 5 6 7 8 8 : turn to:
	 	  case'2':
		    if(($curr_page-1)<=1){$pt= 1;}else{$pt=$curr_page-1;}
		    if(($curr_page+1)>=$pages){$pd=$pages;}else{$pd=$curr_page+1;}
		  	$multipage .= "<b>Page $curr_page of $pages | Items ";

		  	if($curr_page=="1"){
		  	   $multipage.=" 1 - $perpage";
		  	}elseif($curr_page==$pages){
		  	   $multipage.=(($curr_page-1)*$perpage+1)." - ".$num;
		  	}else{
		  	   $multipage.= (($curr_page-1)*$perpage+1)." - ".$curr_page*$perpage;
		  	}

		  	$multipage.="</b> <a href=\"$mpurl1&page=1$mpurl2\" style=\"line-height: normal\">Main page</a>&nbsp;&nbsp;&nbsp;
			<a href=\"$mpurl1&page=$pt$mpurl2\" style=\"line-height: normal\">Previous</a>&nbsp;&nbsp;&nbsp;
			<a href=\"$mpurl1&page=$pd$mpurl2\" style=\"line-height: normal\">Next</a>&nbsp;&nbsp;&nbsp;
			<a href=\"$mpurl1&page=$pages$mpurl2\" style=\"line-height: normal\">Last</a>";
		  break;



		//样式4
		case'4':
			if(($curr_page-1)<=1){$pt= 1;}else{$pt=$curr_page-1;}
		    if(($curr_page+1)>=$pages){$pd=$pages;}else{$pd=$curr_page+1;}
		  	$multipage .= "<b>页面 $curr_page 到 $pages | 操作： ";

		  	if($curr_page=="1"){
		  	//   $multipage.=" 1 - $perpage";
		  	}elseif($curr_page==$pages){
		  //	   $multipage.=(($curr_page-1)*$perpage+1)." - ".$num;
		  	}else{
		  //	   $multipage.= (($curr_page-1)*$perpage+1)." - ".$curr_page*$perpage;
		  	}

		  	$multipage.="</b> <a href=\"$mpurl1&page=1$mpurl2\" style=\"line-height: normal\" target=\"messageFrame\">首页</a>&nbsp;&nbsp;&nbsp;
			<a href=\"$mpurl1&page=$pt$mpurl2\" style=\"line-height: normal\" target=\"messageFrame\">上一页</a>&nbsp;&nbsp;&nbsp;
			<a href=\"$mpurl1&page=$pd$mpurl2\" style=\"line-height: normal\" target=\"messageFrame\">下一页</a>&nbsp;&nbsp;&nbsp;
			<a href=\"$mpurl1&page=$pages$mpurl2\" style=\"line-height: normal\" target=\"messageFrame\">尾页</a>";

		  break;
		
		//Page 1 of 2 | Items 1 - 10 首 页 上一页 下一页 末 页
		//totally   passages/10 perpage homepage previous page next page/last page
		  case'3':
		    if(($curr_page-1)<=1){$pt= 1;}else{$pt=$curr_page-1;}
		    if(($curr_page+1)>=$pages){$pd=$pages;}else{$pd=$curr_page+1;}
		  	$multipage .= "<b>页面 $curr_page 到 $pages | 操作： ";

		  	if($curr_page=="1"){
		  	//   $multipage.=" 1 - $perpage";
		  	}elseif($curr_page==$pages){
		  //	   $multipage.=(($curr_page-1)*$perpage+1)." - ".$num;
		  	}else{
		  //	   $multipage.= (($curr_page-1)*$perpage+1)." - ".$curr_page*$perpage;
		  	}

		  	$multipage.="</b> <a href=\"$mpurl1&page=1$mpurl2\" style=\"line-height: normal\">首页</a>&nbsp;&nbsp;&nbsp;
			<a href=\"$mpurl1&page=$pt$mpurl2\" style=\"line-height: normal\">上一页</a>&nbsp;&nbsp;&nbsp;
			<a href=\"$mpurl1&page=$pd$mpurl2\" style=\"line-height: normal\">下一页</a>&nbsp;&nbsp;&nbsp;
			<a href=\"$mpurl1&page=$pages$mpurl2\" style=\"line-height: normal\">尾页</a>";
			break;
			
			case'html':
				global $siteurl,$data_dir,$cid;
				
				//分类id
				$cate = ((int)$cid>0)?$cid.'_':'';
				
				$data_dir = $siteurl.$data_dir;
				//样式htm
				$multipage .= "<b>分页信息:</b> \n";
				if(($curr_page-5)<=1){$pt= 1;}else{$pt=$curr_page-5;}
				$multipage .= "<a style='color: #006AC2;' href=\"$data_dir/".$cate."list_$pt.html\"><<</a> \n";
				for($i = $from; $i <= $to; $i++) {
					if($i != $curr_page) {
						$multipage .= " <a style='color: #006AC2;' href=\"$data_dir/".$cate."list_$i.html\">$i</a> \n";
					} else {
						$multipage .= "<font color='#006AC2'><strong>[$i]</strong></font> \n";
					}
				}
				if(($curr_page+5)>=$pages){$pd= $pages;}else{$pd=$curr_page+5;}
				$multipage .= "<a style='color: #006AC2;' href=\"$data_dir/".$cate."list_$pd.html\">>></a> \n";
				
			break;
			
			
			
			default:
			//样式3
			//case 3
			//[ 页次: 6/20 页, 共有 192 条信息, 每页 10 条信息 ] 9 7 4 5 6 7 8 8 : 转到:{}
			//[ page: 6/20 , totally 192 messages,  10 messages each page ] 9 7 4 5 6 7 8 8 : turn to
				$multipage .= "<b>分页信息:</b> \n";
				if(($curr_page-5)<=1){$pt= 1;}else{$pt=$curr_page-5;}
				$multipage .= "<a style='color: #006AC2;' href=\"$mpurl1&page=$pt$mpurl2\"><<</a> \n";
				for($i = $from; $i <= $to; $i++) {
					if($i != $curr_page) {
						$multipage .= " <a style='color: #006AC2;' href=\"$mpurl1&page=$i$mpurl2\">$i</a> \n";
					} else {
						$multipage .= "<font color='#006AC2'><strong>[$i]</strong></font> \n";
					}
				}
				if(($curr_page+5)>=$pages){$pd= $pages;}else{$pd=$curr_page+5;}
				$multipage .= "<a style='color: #006AC2;' href=\"$mpurl1&page=$pd$mpurl2\">>></a> \n";
			  break;
		}
	}else{
		$multipage .= '<b>分页信息: </b>目前只有一页';
	}

	//检测是否需要连接控制
	if(trim($target)!=''){
		$multipage  = str_replace("<a ","<a target='".$target."' ",$multipage);
	}
  return $multipage;
}
?>